package com.contory.ssn_lgnd.app.title;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.List;
import java.util.Random;

import android.opengl.GLES20;
import android.util.Log;

import com.contory.ssn_lgnd.app.Assets;
import com.contory.ssn_lgnd.app.ESShaderHandleNames;
import com.contory.ssn_lgnd.app.EffectHelper_Blur;
import com.contory.ssn_lgnd.app.EffectHelper_Default;
import com.contory.ssn_lgnd.app.EffectHelper_Glow;
import com.contory.ssn_lgnd.app.Settings;
import com.contory.ssn_lgnd.app.comp.ViewTestObject;
import com.contory.ssn_lgnd.app.comp.ButtonObject;
import com.contory.ssn_lgnd.app.comp.ToggleObject;
import com.contory.ssn_lgnd.app.comp.SoundPtr;
import com.contory.ssn_lgnd.app.comp.ViewObject;
import com.contory.ssn_lgnd.app.maingame.Curtain;
import com.contory.ssn_lgnd.app.title.TitleStageSelectButtons.TitleStageSelectBtrChd;
import com.contory.ssn_lgnd.framework.Game;
import com.contory.ssn_lgnd.framework.Input.TouchEvent;
import com.contory.ssn_lgnd.framework.gl.Camera2D;
import com.contory.ssn_lgnd.framework.gl.ESShader;
import com.contory.ssn_lgnd.framework.gl.ESShaderLoader;
import com.contory.ssn_lgnd.framework.gl.FrameBufferObject;
import com.contory.ssn_lgnd.framework.gl.SpriteBatcher;
import com.contory.ssn_lgnd.framework.gl.TextureRegion;
import com.contory.ssn_lgnd.framework.gl.Texture_NoBitmapBuffered;
import com.contory.ssn_lgnd.framework.impl.GLGame;
import com.contory.ssn_lgnd.framework.impl.GLScreen;
import com.contory.ssn_lgnd.framework.math.Vector2;

public class Title extends GLScreen{
	FrameBufferObject fb1;
	FrameBufferObject fb2;
	FrameBufferObject fb3;
	
	ESShaderLoader el_vtr;
	ESShaderLoader el_frg;
	
	ESShader shader_default;
	ESShader shader_blur;
	ESShader shader_glow;
	ESShader shader_curtain;
	SpriteBatcher batcher;
	Camera2D cam;
	
	EffectHelper_Default fx_glow_defalutRender;
	EffectHelper_Blur fx_glow_blurRender;
	EffectHelper_Glow fx_glow_additiveRender;
	EffectHelper_Default fx_norm_defaultRender;
	
	Vector2 touchPoint = new Vector2();

	ViewTestObject sky;
	ViewTestObject sea;
	ViewTestObject sun;
	ViewTestObject rock;
	TitlePlayerObject player;
	TitleCloudObject cloud;
	TitleWaveObject wave;
	TitleLetterObject titleLetter;
	ButtonObject startBtr;
	ToggleObject musicBtr;
	ToggleObject effectBtr;
	TitleStageSelectButtons stageBtrs;
	Curtain curtain;
	
	TitleScene state_end;
	TitleScene state_next;
	TitleScene state_stage;
	TitleScene state_opening;
	TitleScene state_title;
	TitleScene state;
	
	float vignettingValue = 0.0f;
	public Title(Game game) {
		super(game);
		fb1 = new FrameBufferObject(512, 256); // blur
		fb2 = new FrameBufferObject(512, 256); // normal render
		fb3 = new FrameBufferObject(1024, 512); // normal render
		el_vtr = new ESShaderLoader();
		el_frg = new ESShaderLoader();
		shader_default = new ESShader();
		shader_blur = new ESShader();
		shader_glow = new ESShader();
		shader_curtain = new ESShader();
		batcher = new SpriteBatcher(100, true);
	    cam = new Camera2D(GLGame.frameWidth, GLGame.frameHeight, 20, 10);
	    
	    fx_glow_defalutRender = new EffectHelper_Default(fb2, shader_default, cam, batcher);
	    fx_glow_blurRender = new EffectHelper_Blur(fb1, shader_blur, cam, batcher);
	    fx_glow_additiveRender = new EffectHelper_Glow(fb1, shader_glow, cam, batcher);
	    fx_norm_defaultRender = new EffectHelper_Default(fb3, shader_default, cam, batcher);
	    
		sky = new ViewTestObject();
		sky.setXY_WH_TR(10, 7.0f, 20, 6, Assets.tex_reg_whiteTexel);
		sky.setRGBA(1, 0.788f, 0.054f, 1);
		
		sun = new ViewTestObject();
		sun.setXY_WH_TR(10, 6, 8, 4, Assets.tex_reg_sun);
		
		sea = new ViewTestObject();
		sea.setXY_WH_TR(10, 2.0f, 20, 5, Assets.tex_reg_whiteTexel);
		sea.setRGBA(0.0f, 0.64f, 0.9f, 1.0f);
		
		player = new TitlePlayerObject();
		
		cloud = new TitleCloudObject();
		
		rock = new ViewTestObject();
		rock.setXY_WH_TR(18.5f, 1.5f, 3, 3, Assets.tex_reg_rock);
		
		wave = new TitleWaveObject();
		
		titleLetter = new TitleLetterObject();
		
		startBtr = new ButtonObject();
		startBtr.setXY_WH_TR(10, 3.5f, 5, 1, Assets.tex_reg_button1);
		startBtr.setString("START");
		startBtr.setSound(Assets.sound_click);
		
		musicBtr = new ToggleObject();
		musicBtr.setXY_WH_TR(1.25f, 1, 1.5f, 1.5f, 
				Assets.tex_reg_button2, Assets.tex_reg_mucisIcon, Assets.tex_reg_disable, Settings.musicEnabled);
		musicBtr.setTodo(new TitleMusicBtrTodo(this, musicBtr));
		musicBtr.setSound(Assets.sound_click);
		
		effectBtr = new ToggleObject();
		effectBtr.setXY_WH_TR(3.25f, 1, 1.5f, 1.5f, 
				Assets.tex_reg_button2, Assets.tex_reg_effectIcon, Assets.tex_reg_disable, Settings.effectEnabled);
		effectBtr.setTodo(new TitleEffectBtrTodo(this, effectBtr));
		effectBtr.setSound(Assets.sound_click);
		
		stageBtrs = new TitleStageSelectButtons(this);
		
		curtain = new Curtain(cam);
		curtain.setCloseCurtain();
		
		state_end = new TitleState_End(this);
		state_next = new TitleState_Next(this);
		state_stage = new TitleState_Stage(this);
		state_opening = new TitleState_Opening(this);
		state_title = new TitleState_Title(this);
		state = state_opening;
		
		Assets.musicReady(glGame, "Opening.mp3");
    }
	
	@Override
	public void update(float deltaTime) {
		game.getInput().getKeyEvents();
		List<TouchEvent> touchEvent = game.getInput().getTouchEvents();
		if(titleLetter.IsAniEnd){
			for(int i = 0; i < touchEvent.size(); i++){
				touchPoint.set(touchEvent.get(i).x, touchEvent.get(i).y);
				cam.touchToWorld(touchPoint);
				state.setTouchEvent(touchEvent.get(i), touchPoint.x, touchPoint.y);
			}
		}
		state.update(deltaTime);
	}

	@Override
	public void present(float deltaTime) {
		state.draw(batcher);
	}

	@Override
	public void pause() {
		Assets.stopBGM(Assets.BGM);
		el_vtr.unload();
		el_frg.unload();
	}

	@Override
	public void resume() {
		fb1.load();
		fb2.load();
		fb3.load();
		
		GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		GLES20.glEnable(GLES20.GL_BLEND);
		GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
		
		el_vtr.load(GLES20.GL_VERTEX_SHADER, Assets.codeImporter.getShaderCode("shd_defualt_vertex.txt"));
		el_frg.load(GLES20.GL_FRAGMENT_SHADER, Assets.codeImporter.getShaderCode("shd_default_fragment.txt"));
		shader_default.load(el_vtr.getShaderId(), el_frg.getShaderId());
		el_frg.load(GLES20.GL_FRAGMENT_SHADER, Assets.codeImporter.getShaderCode("shd_blur_fragment.txt"));
		shader_blur.load(el_vtr.getShaderId(), el_frg.getShaderId());
		el_frg.load(GLES20.GL_FRAGMENT_SHADER, Assets.codeImporter.getShaderCode("shd_glow_fragment.txt"));
		shader_glow.load(el_vtr.getShaderId(), el_frg.getShaderId());
		el_frg.load(GLES20.GL_FRAGMENT_SHADER, Assets.codeImporter.getShaderCode("shd_curtain_fragment.txt"));
		shader_curtain.load(el_vtr.getShaderId(), el_frg.getShaderId());
		
		Assets.playBGM(Assets.BGM);
	}

	@Override
	public void dispose() {
	}
	
	public GLGame getGLGame(){
		return glGame;
	}
}
